home *** CD-ROM | disk | FTP | other *** search
/ Programming a Multiplayer FPS in DirectX / Programming a Multiplayer FPS in DirectX (Companion CD).iso / Source / Chapter 3 / Engine / Engine.cpp next >
Encoding:
C/C++ Source or Header  |  2005-03-29  |  7.8 KB  |  260 lines

  1. //-----------------------------------------------------------------------------
  2. // Engine.h implementation.
  3. // Refer to the Engine.h interface for more details.
  4. //
  5. // Programming a Multiplayer First Person Shooter in DirectX
  6. // Copyright (c) 2004 Vaughan Young
  7. //-----------------------------------------------------------------------------
  8. #include "Engine.h"
  9.  
  10. //-----------------------------------------------------------------------------
  11. // Globals
  12. //-----------------------------------------------------------------------------
  13. Engine *g_engine = NULL;
  14.  
  15. //-----------------------------------------------------------------------------
  16. // Handles Windows messages.
  17. //-----------------------------------------------------------------------------
  18. LRESULT CALLBACK WindowProc( HWND wnd, UINT msg, WPARAM wparam, LPARAM lparam )
  19. {
  20.     switch( msg )
  21.     {
  22.         case WM_ACTIVATEAPP:
  23.             g_engine->SetDeactiveFlag( !wparam );
  24.             return 0;
  25.  
  26.         case WM_DESTROY:
  27.             PostQuitMessage( 0 );
  28.             return 0;
  29.  
  30.         default:
  31.             return DefWindowProc( wnd, msg, wparam, lparam );
  32.     }
  33. }
  34.  
  35. //-----------------------------------------------------------------------------
  36. // The engine class constructor.
  37. //-----------------------------------------------------------------------------
  38. Engine::Engine( EngineSetup *setup )
  39. {
  40.     // Indicate that the engine is not yet loaded.
  41.     m_loaded = false;
  42.  
  43.     // If no setup structure was passed in, then create a default one.
  44.     // Otehrwise, make a copy of the passed in structure.
  45.     m_setup = new EngineSetup;
  46.     if( setup != NULL )
  47.         memcpy( m_setup, setup, sizeof( EngineSetup ) );
  48.  
  49.     // Store a pointer to the engine in a global variable for easy access.
  50.     g_engine = this;
  51.  
  52.     // Prepare and register the window class.
  53.     WNDCLASSEX wcex;
  54.     wcex.cbSize        = sizeof( WNDCLASSEX );
  55.     wcex.style         = CS_CLASSDC;
  56.     wcex.lpfnWndProc   = WindowProc;
  57.     wcex.cbClsExtra    = 0;
  58.     wcex.cbWndExtra    = 0;
  59.     wcex.hInstance     = m_setup->instance;
  60.     wcex.hIcon         = LoadIcon( NULL, IDI_APPLICATION );
  61.     wcex.hCursor       = LoadCursor( NULL, IDC_ARROW );
  62.     wcex.hbrBackground = NULL;
  63.     wcex.lpszMenuName  = NULL;
  64.     wcex.lpszClassName = "WindowClass";
  65.     wcex.hIconSm       = LoadIcon( NULL, IDI_APPLICATION );
  66.     RegisterClassEx( &wcex );
  67.  
  68.     // Initialise the COM using multithreaded concurrency.
  69.     CoInitializeEx( NULL, COINIT_MULTITHREADED );
  70.  
  71.     // Create the window and retrieve a handle to it.
  72.     // Note: Later the window will be created using a windowed/fullscreen flag.
  73.     m_window = CreateWindow( "WindowClass", m_setup->name, WS_OVERLAPPED, 0, 0, 800, 600, NULL, NULL, m_setup->instance, NULL );
  74.  
  75.     // Create the linked lists of states.
  76.     m_states = new LinkedList< State >;
  77.     m_currentState = NULL;
  78.  
  79.     // Create the input object.
  80.     m_input = new Input( m_window );
  81.  
  82.     // Seed the random number generator with the current time.
  83.     srand( timeGetTime() );
  84.  
  85.     // Allow the application to perform any state setup now.
  86.     if( m_setup->StateSetup != NULL )
  87.         m_setup->StateSetup();
  88.  
  89.     // The engine is fully loaded and ready to go.
  90.     m_loaded = true;
  91. }
  92.  
  93. //-----------------------------------------------------------------------------
  94. // The engine class destructor.
  95. //-----------------------------------------------------------------------------
  96. Engine::~Engine()
  97. {
  98.     // Ensure the engine is loaded.
  99.     if( m_loaded == true )
  100.     {
  101.         // Destroy the states linked lists.
  102.         if( m_currentState != NULL )
  103.             m_currentState->Close();
  104.         SAFE_DELETE( m_states );
  105.  
  106.         // Destroy everything.
  107.         SAFE_DELETE( m_input );
  108.     }
  109.  
  110.     // Uninitialise the COM.
  111.     CoUninitialize();
  112.  
  113.     // Unregister the window class.
  114.     UnregisterClass( "WindowClass", m_setup->instance );
  115.  
  116.     // Destroy the engine setup structure.
  117.     SAFE_DELETE( m_setup );
  118. }
  119.  
  120. //-----------------------------------------------------------------------------
  121. // Enters the engine into the main processing loop.
  122. //-----------------------------------------------------------------------------
  123. void Engine::Run()
  124. {
  125.     // Ensure the engine is loaded.
  126.     if( m_loaded == true )
  127.     {
  128.         // Show the window.
  129.         ShowWindow( m_window, SW_NORMAL );
  130.  
  131.         // Used to retrieve details about the viewer from the application.
  132.         ViewerSetup viewer;
  133.  
  134.         // Enter the message loop.
  135.         MSG msg;
  136.         ZeroMemory( &msg, sizeof( MSG ) );
  137.         while( msg.message != WM_QUIT )
  138.         {
  139.             if( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) )
  140.             {
  141.                 TranslateMessage( &msg );
  142.                 DispatchMessage( &msg );
  143.             }
  144.             else if( !m_deactive )
  145.             {
  146.                 // Calculate the elapsed time.
  147.                 unsigned long currentTime = timeGetTime();
  148.                 static unsigned long lastTime = currentTime;
  149.                 float elapsed = ( currentTime - lastTime ) / 1000.0f;
  150.                 lastTime = currentTime;
  151.  
  152.                 // Update the input object, reading the keyboard and mouse.
  153.                 m_input->Update();
  154.  
  155.                 // Check if the user wants to make a forced exit.
  156.                 if( m_input->GetKeyPress( DIK_F1 ) )
  157.                     PostQuitMessage( 0 );
  158.  
  159.                 // Request the viewer from the current state, if there is one.
  160.                 if( m_currentState != NULL )
  161.                     m_currentState->RequestViewer( &viewer );
  162.  
  163.                 // Update the current state (if there is one), taking state
  164.                 // changes into account.
  165.                 m_stateChanged = false;
  166.                 if( m_currentState != NULL )
  167.                     m_currentState->Update( elapsed );
  168.                 if( m_stateChanged == true )
  169.                     continue;
  170.             }
  171.         }
  172.     }
  173.  
  174.     // Destroy the engine.
  175.     SAFE_DELETE( g_engine );
  176. }
  177.  
  178. //-----------------------------------------------------------------------------
  179. // Returns the window handle.
  180. //-----------------------------------------------------------------------------
  181. HWND Engine::GetWindow()
  182. {
  183.     return m_window;
  184. }
  185.  
  186. //-----------------------------------------------------------------------------
  187. // Sets the deactive flag.
  188. //-----------------------------------------------------------------------------
  189. void Engine::SetDeactiveFlag( bool deactive )
  190. {
  191.     m_deactive = deactive;
  192. }
  193.  
  194. //-----------------------------------------------------------------------------
  195. // Adds a state to the engine.
  196. //-----------------------------------------------------------------------------
  197. void Engine::AddState( State *state, bool change )
  198. {
  199.     m_states->Add( state );
  200.  
  201.     if( change == false )
  202.         return;
  203.  
  204.     if( m_currentState != NULL )
  205.         m_currentState->Close();
  206.  
  207.     m_currentState = m_states->GetLast();
  208.     m_currentState->Load();
  209. }
  210.  
  211. //-----------------------------------------------------------------------------
  212. // Removes a state from the engine
  213. //-----------------------------------------------------------------------------
  214. void Engine::RemoveState( State *state )
  215. {
  216.     m_states->Remove( &state );
  217. }
  218.  
  219. //-----------------------------------------------------------------------------
  220. // Changes processing to the state with the specified ID.
  221. //-----------------------------------------------------------------------------
  222. void Engine::ChangeState( unsigned long id )
  223. {
  224.     // Iterate through the list of states and find the new state to change to.
  225.     m_states->Iterate( true );
  226.     while( m_states->Iterate() != NULL )
  227.     {
  228.         if( m_states->GetCurrent()->GetID() == id )
  229.         {
  230.             // Close the old state.
  231.             if( m_currentState != NULL )
  232.                 m_currentState->Close();
  233.  
  234.             // Set the new current state and load it.
  235.             m_currentState = m_states->GetCurrent();
  236.             m_currentState->Load();
  237.  
  238.             // Indicate that the state has changed.
  239.             m_stateChanged = true;
  240.  
  241.             break;
  242.         }
  243.     }
  244. }
  245.  
  246. //-----------------------------------------------------------------------------
  247. // Returns a pointer to the current state.
  248. //-----------------------------------------------------------------------------
  249. State *Engine::GetCurrentState()
  250. {
  251.     return m_currentState;
  252. }
  253.  
  254. //-----------------------------------------------------------------------------
  255. // Returns a pointer to the input object.
  256. //-----------------------------------------------------------------------------
  257. Input *Engine::GetInput()
  258. {
  259.     return m_input;
  260. }